# -*- coding: utf-8 -*-

# @File  : t_supplier.py
# @Author: Lomo
# @Site  : lomo.space
# @Date  : 2019-11-27
# @Desc  : 供应商信息

from .db import DB


class TSupplier(object):
    def __init__(self):
        self.connect = DB.connect()
        self.cur = self.connect.cursor()
        self.table_name = 't_supplier'

    def get_type_by_supplier_id(self, supplier_id):
        """
        获取供应商合作类型: 1->自营, 2->POP
        :param supplier_id: 供应商 id
        :return: int: 合作类型
        """
        sql = "select coop_type from {0} where id={1}".format(self.table_name, supplier_id)
        self.cur.execute(sql)
        return self.cur.fetchone()

    @staticmethod
    def get_coop_type_by_id(id_):
        """
        获取某个供应商合作类型: 1->自营, 2->POP
        :param id_: supplier_id
        :return: int, 1/2
        """
        res_type = TSupplier().get_type_by_supplier_id(supplier_id=id_)
        if res_type is not None:
            return res_type[0]
        else:
            return False

    def get_total_count_by_type(self, coop_type):
        sql = "select count(*) from {0} where coop_type={1}".format(self.table_name, coop_type)
        self.cur.execute(sql)
        return self.cur.fetchone()

    @staticmethod
    def get_totals_by_type(coop_type):
        """
        获取 pop/自营 供应商总数
        :param coop_type:
        :return:
        """
        totals = TSupplier().get_total_count_by_type(coop_type=coop_type)
        if totals is not None:
            return totals[0]
        else:
            return False



